package com.edevp.common.core.util.session;

/*import com.edevp.core.component.auth.dto.AuthTokenDTO;
import com.edevp.core.constant.CommonConstants;
import com.edevp.core.dto.R;
import com.edevp.core.exception.EngineException;
import com.edevp.core.exception.ExceptionCode;
import com.edevp.core.util.CommonUtil;
import com.edevp.plugin.i18n.LocalUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpStatus;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;*/
import cn.hutool.core.text.AntPathMatcher;
import com.edevp.common.core.constant.CommonConstants;
import com.edevp.common.core.dto.R;
import com.edevp.common.core.exception.EngineException;
import com.edevp.common.core.exception.ExceptionCode;
import com.edevp.common.core.util.CommonUtil;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Optional;
import java.util.Set;

/**
 * ${DESCRIPTION}
 *
 * @author Blueeyedboy
 * @create 2019-04-08 4:46 PM
 **/
public class SessionUtil {

	private static final String TOKEN = "x_token";
	/*
	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();


	public static String getUserId(){
		AuthTokenDTO token = getToken();
		if(token != null){
			return token.getUserId();
		}
		return null;
	}
	public static String getUserName(){
		AuthTokenDTO token = getToken();
		if(token != null){
			return token.getUserName();
		}
		return null;
	}
	public static String getComId(){
		return null;
	}
	public static String getSubComId(){
		return null;
	}

	public static boolean isAdmin(){
		AuthTokenDTO token = getToken();
		if(token != null){
			Set<String> roles =  token.getRoles();
			if(roles != null && roles.contains(CommonConstants.ROLE_ADMIN)){
				return true;
			}
		}
		return false;
	}

	public static void setToken(AuthTokenDTO token){
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if(requestAttributes != null){
			token.setToken(null);
			token.setRefreshToken(null);
			requestAttributes.setAttribute(TOKEN,token,0);
		}
	}

	public static AuthTokenDTO getToken(){
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if(requestAttributes != null){
			Object o = requestAttributes.getAttribute(TOKEN,0);
			if(o != null){
				return (AuthTokenDTO)o;
			}
		}
		return null;
	}
	*/
	/**
	 * 获取客户端IP
	 * @return
	 * @author Blueeyedboy
	 * @create 2017/6/7 15:21
	 */
	public static String getClientIpAddr() {
		HttpServletRequest request = getCurrentRequest();
		if (request == null) {
			return "127.0.0.1";
		}
		String ip = request.getHeader("x-forwarded-for");

		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if(CommonUtil.isNull(ip)){
			return "127.0.0.1";
		}
		return ip;
	}
	/*
	public static AntPathMatcher antPathMatcher = new AntPathMatcher();

	public static AntPathMatcher getAntPathMatcher() {
		return antPathMatcher;
	}
*/
	/**
	 * 从当前线程获取请求
	 * @return javax.servlet.http.HttpServletRequest
	 * @author Lean
	 * @create 2017/6/7 15:21
	 **/
	public static HttpServletRequest getCurrentRequest() {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		if(requestAttributes != null){
			ServletRequestAttributes attributes = (ServletRequestAttributes)requestAttributes;
			return attributes.getRequest();
		}
		return null;
	}
	public static String getCurrentRequestURI(){
		HttpServletRequest request = getCurrentRequest();
		return Optional.ofNullable(request).map(HttpServletRequest::getServletPath).orElse(null);
	}

	/*
	public static void writeJson(HttpServletResponse response, R r){
		response.setCharacterEncoding(CommonConstants.UTF8);
		response.setContentType(CommonConstants.CONTENT_TYPE);
		response.setStatus(HttpStatus.OK.value());
		PrintWriter printWriter = null;
		try {
			if(r.getMsg() == null){
				r.setMsg(LocalUtil.get(r.getCode()));
			}
			printWriter = response.getWriter();
			printWriter.append(OBJECT_MAPPER.writeValueAsString(r));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static void writeExceptionJson(HttpServletResponse response,int code){
		writeExceptionJson(response,code,LocalUtil.get(code));
	}

	public static void writeExceptionJson(HttpServletResponse response,int code,String message){
		R<String> r = new R<>();
		r.setCode(code);
		r.setMsg(message);
		writeJson(response,r);
	}
	
	public static boolean checkCreateUser(String id){
		String userId = getUserId();
		if(CommonUtil.isNotNull(userId)){
			if(CommonUtil.isNull(id) || !id.equals(userId)){
				throw new EngineException(ExceptionCode.AUTH_NOT_CREATED);
			}
		}
		return true;
	}*/

}
